// 2013-7-29下午06:21:38  net.betou.trade.main.manager.impl.VideoMngImpl.java

package net.betou.trade.main.manager.impl;

import java.util.List;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.core.entity.ConfigLogin;
import net.betou.core.service.PostDataListener;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Video;
import net.betou.trade.main.dao.VideoDao;
import net.betou.trade.main.manager.VideoMng;
import net.betou.trade.role.setting.CommonFinderConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class VideoMngImpl implements VideoMng{

	public Video deleteById( Integer id ) {
		Video bean = dao.deleteById( id );
		afterDelete( bean );
		
		return bean;
	}

	public Video [ ] deleteByIds( Integer [ ] ids ) {
		Video [ ] beans = new Video[ ids.length ];
		
		for( int i = 0; i < ids.length; i++ ){
			beans[ i ] = deleteById( ids[ i ] );
		}
		return beans;
	}

	@Transactional( readOnly = true )
	public Video findById( Integer id ) {
		return dao.findById( id );
	}
	
	@Transactional( readOnly = true )
	public Video findMyVideo( Integer id, Integer selfId, Boolean system ) {
		Video notice;
		if( system ){
			notice = dao.findById( id );
		}else{
			notice = dao.findMyVideo( id, selfId );
		}
		
		return notice;
	}
	
	@Transactional( readOnly = true )
	public Pagination getPage( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection,  int pageNo, int pageSize ) {
		Boolean system = admin.getSystem( );
		Pagination pagination;
		
		if( system ){
			pagination = dao.getPage( null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
		}else{
			
			CommonFinderConfig config = new CommonFinderConfig( admin, ConfigLogin.CONFIG_VIDEO );
			
			if( config.isSelf( ) ){
				pagination = dao.getPage( admin.getId( ), searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize );
			}else{
				pagination = dao.getPageByCriteria( searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, config.getDeepSQL( ) );
			}
		}
		
		return pagination;
	}

	public Video save( Video bean, AgentAndAdmins admin ) {
		bean.init( );
		
		bean.setAdmin( admin );
		
		dao.save( bean );
		
		afterSave( bean );
		return bean;
	}

	public Video update( Video bean ) {
		bean.initUpdate( );
		
		Updater< Video > updater = new Updater< Video >( bean, Updater.UpdateMode.MIN ); 
		updater.include( "title" ); // 使用最小模式
		updater.include( "description" );
		updater.include( "path" );
		updater.include( "img" );
		return dao.updateByUpdater( updater );
	}
	
	private void afterSave( Video notice ) {
		if( listenerList != null ){
			for ( PostDataListener< Video > listener : listenerList ){
				listener.afterSave( notice );
			}
		}
	}
	
	private void afterDelete( Video notice ){
		if( listenerList != null ){
			for ( PostDataListener< Video > listener : listenerList ){
				listener.afterDelete( notice );
			}
		}
	}
	
	private List< PostDataListener< Video > > listenerList;

	public void setListenerList( List< PostDataListener< Video > > listenerList ) {
		this.listenerList = listenerList;
	}
	
	@Autowired
	private VideoDao dao;
	

}
